package com.leo.springbootmultithreadbasic.synchronizedkey.synchronizedmethod.accessinstance;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Leo
 * @version V1.0
 */
public class MyService {

    private static final Logger logger = LoggerFactory.getLogger(MyService.class);

    // 共享的变量
    private int num = 0;

    /**
     * 对一个成员变量赋值
     *
     * @param threadName
     */
    synchronized public void setNum(String threadName)  {
        if ("a".equals(threadName)) {
            num = 100;
            logger.info("a set over");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if ("b".equals(threadName)) {
            num = 200;
            logger.info("b set over");
        }

        logger.info("threadName: {}, num={}", threadName, num);
    }
}
